<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="field[@type = 'foreign_key' and parent::doctype]">
		<xsl:variable name="is_new" select="parent::doctype/@action = 'create'"/>
		<xsl:variable name="document_field" select="../../document[@doctype_name = current()/parent::doctype/@name]/field[@name = current()/@name]"/>
		<div class="field">
			<label class="title_label" for="{@name}">
				<xsl:value-of select="@title"/>
				<xsl:text>:&#160;</xsl:text>
				<xsl:if test="@is_important = 1">
					<span class="star">*</span>
				</xsl:if>
			</label>
			<select name="{@name}">
				<xsl:if test="$is_new or not(@is_important = 1)">
					<option value="">
						<xsl:apply-templates mode="dtf_foreign_key_not_selected" select="." />
					</option>
				</xsl:if>
				<xsl:for-each select="item">
					<option value="{@value}">
						<xsl:choose>
							<xsl:when test="../../../pass_info/vars/var[@name = current()/../@name] = @value">
								<xsl:attribute name="selected">selected</xsl:attribute>
							</xsl:when>
							<xsl:when test="../../../pass_info/vars/var[@name = current()/../@name]"/>
							<xsl:when test="$document_field/@value_key = @value">
								<xsl:attribute name="selected">selected</xsl:attribute>
							</xsl:when>
						</xsl:choose>
						<xsl:value-of select="@title"/>
					</option>
				</xsl:for-each>
			</select>
			<xsl:apply-templates mode="dtf_error" select="." />
			<xsl:if test="@comment != ''">
				<div class="comment-">
					<xsl:value-of select="@comment"/>
				</div>
			</xsl:if>
		</div>
	</xsl:template>
</xsl:stylesheet>
